<template>
  <div class="user-center">
    <div class="profile-header">
      <div class="profile-banner"></div>
      <div class="profile-avatar-container">
        <div class="profile-avatar">{{ userInitial }}</div>
        <div class="upload-hint">点击上传头像</div>
      </div>
      <div class="profile-name">
        <h1>{{ displayName }}</h1>
        <span class="edit-icon" @click="startEditName">✎</span>
      </div>
    </div>

    <div class="profile-content">
      <div class="profile-card">
        <div class="card-header">
          <h2>个人资料</h2>
          <button class="edit-button" @click="toggleEdit">
            {{ isEditing ? '保存' : '编辑' }}
          </button>
        </div>

        <div class="info-list">
          <div class="info-item">
            <label>用户名</label>
            <div v-if="!isEditing" class="info-value">{{ displayName }}</div>
            <input 
              v-else 
              v-model="editingName" 
              class="edit-input"
              placeholder="请输入用户名"
            />
          </div>

          <div class="info-item">
            <label>邮箱</label>
            <div class="info-value">{{ userEmail }}</div>
          </div>

          <div class="info-item">
            <label>注册时间</label>
            <div class="info-value">{{ registrationDate }}</div>
          </div>
        </div>

        <div class="action-buttons">
          <button class="primary-button" @click="goToTodo">
            <span class="icon">📝</span>
            进入Todo
          </button>
          <button class="logout-button" @click="handleLogout">
            <span class="icon">🚪</span>
            退出登录
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const userEmail = ref('')
const userName = ref('')
const isEditing = ref(false)
const editingName = ref('')
const registrationDate = ref(new Date().toLocaleDateString())

// 生成默认用户名
const generateDefaultName = (email) => {
  return `用户${Math.floor(Math.random() * 10000)}`
}

const displayName = computed(() => {
  return userName.value || generateDefaultName(userEmail.value)
})

const userInitial = computed(() => {
  return displayName.value ? displayName.value[0].toUpperCase() : 'U'
})

onMounted(() => {
  const currentUser = JSON.parse(localStorage.getItem('currentUser'))
  if (currentUser) {
    userEmail.value = currentUser.email
    userName.value = currentUser.name || ''
    editingName.value = userName.value
  } else {
    router.push('/login')
  }
})

function toggleEdit() {
  if (isEditing.value) {
    // 保存编辑
    userName.value = editingName.value
    const currentUser = JSON.parse(localStorage.getItem('currentUser'))
    currentUser.name = editingName.value
    localStorage.setItem('currentUser', JSON.stringify(currentUser))
  }
  isEditing.value = !isEditing.value
}

function startEditName() {
  isEditing.value = true
}

function goToTodo() {
  router.push('/todo')
}

function handleLogout() {
  localStorage.removeItem('currentUser')
  router.push('/login')
}
</script>

<style scoped>
.user-center {
  min-height: 100vh;
  background: #f0f2f5;
}

.profile-header {
  position: relative;
  height: 300px;
  text-align: center;
}

.profile-banner {
  height: 200px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.profile-avatar-container {
  position: relative;
  display: inline-block;
  margin-top: -60px;
}

.profile-avatar {
  width: 120px;
  height: 120px;
  background: linear-gradient(45deg, #6c63ff, #3071b3);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 48px;
  border: 4px solid white;
  cursor: pointer;
  transition: all 0.3s ease;
}

.upload-hint {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: #666;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.profile-avatar-container:hover .upload-hint {
  opacity: 1;
}

.profile-name {
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.profile-name h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.edit-icon {
  cursor: pointer;
  color: #666;
  font-size: 18px;
  transition: color 0.3s ease;
}

.edit-icon:hover {
  color: #6c63ff;
}

.profile-content {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.profile-card {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.card-header h2 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.edit-button {
  padding: 8px 16px;
  border: 1px solid #6c63ff;
  border-radius: 6px;
  background: transparent;
  color: #6c63ff;
  cursor: pointer;
  transition: all 0.3s ease;
}

.edit-button:hover {
  background: #6c63ff;
  color: white;
}

.info-list {
  margin-bottom: 30px;
}

.info-item {
  margin-bottom: 20px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 10px;
}

.info-item label {
  display: block;
  color: #666;
  font-size: 14px;
  margin-bottom: 5px;
}

.info-value {
  color: #333;
  font-size: 16px;
}

.edit-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
}

.action-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.action-buttons button {
  padding: 12px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.primary-button {
  background: linear-gradient(45deg, #6c63ff, #3071b3);
  color: white;
}

.primary-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(108, 99, 255, 0.3);
}

.logout-button {
  background: #f8f9fa;
  color: #dc3545;
}

.logout-button:hover {
  background: #dc3545;
  color: white;
}

.icon {
  font-size: 18px;
}

@media (max-width: 768px) {
  .profile-content {
    padding: 10px;
  }

  .profile-card {
    padding: 20px;
  }

  .action-buttons {
    grid-template-columns: 1fr;
  }
}
</style> 